package com.zelin.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zelin.manager.dao.SpecificationOptionDao;
import com.zelin.manager.entity.SpecificationOptionEntity;
import com.zelin.manager.group.Specification;
import com.zelin.manager.service.SpecificationOptionService;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.zelin.manager.dao.SpecificationDao;
import com.zelin.manager.entity.SpecificationEntity;
import com.zelin.manager.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {
    @Autowired
    private SpecificationOptionService optionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SpecificationEntity> queryWrapper = new QueryWrapper<>();

        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }


    /**
     * 功能: 添加规格(同时添加规格选项)
     * 参数:
     * 返回值: void
     * 时间: 2021/10/25 17:00
     */
    @Override
    public void insert(Specification specification) {
        //1. 得到规格对象
        SpecificationEntity spec = specification.getSpec();
        //2. 添加规格对象,此时规格就有了主键值（具体说：spec这个对象有了主键值）
        baseMapper.insert(spec);
        System.out.println("spec = " + spec);
        //3. 添加规格选项
        for (SpecificationOptionEntity option : specification.getOptions()) {
            //3.1 需要绑定外键
            option.setSpecId(spec.getId());
            //3.2 添加规格选项到数据库中
            optionService.save(option);
        }

    }

    /**
     * 功能: 根据规格id查询规格及规格选项这个复合对象
     * 参数:
     * 返回值: com.zelin.manager.group.Specification
     * 时间: 2021/10/25 17:28
     */
    @Override
    public Specification findById(Long id) {
        //1. 根据规格id，查询规格
        SpecificationEntity spec = baseMapper.selectById(id);
        //2. 再根据规格id查询规格选项列表
        //2.1 封装查询条件
        QueryWrapper<SpecificationOptionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_id",id);
        //2.2 根据外键查询得到规格选项列表
        List<SpecificationOptionEntity> options = optionService.list(wrapper);
        //3. 定义组合对象
        Specification specification = new Specification();
        specification.setSpec(spec);
        specification.setOptions(options);
        return specification;
    }

    /**
     * 功能: 修改规格及规格选项
     * 参数:
     * 返回值: void
     * 时间: 2021/10/26 14:34
     */
    @Override
    public void update(Specification specification) {
        //1. 修改规格
        SpecificationEntity spec = specification.getSpec();
        baseMapper.updateById(spec);

        //2. 根据规格id在多的一方的表中删除记录（tb_specification_option）
        optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",spec.getId()));

        //3. 添加规格选项数据到tb_specification_option表中
        List<SpecificationOptionEntity> options = specification.getOptions();
        for (SpecificationOptionEntity option : options) {
            //3.1 绑定规格选项的外键
            option.setSpecId(spec.getId());
            //3.2 添加数据到规格选项表中
            optionService.save(option);
        }
    }

    /**
     * 功能: 根据id删除规格及规格选项
     * 参数:
     * 返回值: void
     * 时间: 2021/10/26 15:57
     */
    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            //1. 删除规格
            baseMapper.deleteById(id);
            //2. 删除规格选项（根据规格的id来删除它）
            optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",id));
        }
    }

}